﻿/// <summary>Description</summary>

$(function () {

    $("#contrast-dialog:ui-dialog").dialog("destroy");


    var value = $("#tbxContrastValue"),
			allFields = $([]).add(value),
			tips = $(".validateTips");


    function updateTips(t) {

        tips
				.text(t)
				.addClass("ui-state-highlight");

        setTimeout(function () {

            tips.removeClass("ui-state-highlight", 1500);

        }, 500);

    }


    function checkLength(o, n, min, max) {

        if (o.val().length > max || o.val().length < min) {

            o.addClass("ui-state-error");

            updateTips("Length of " + n + " must be between " +
    					min + " and " + max + ".");

            return false;

        } else {

            return true;

        }

    }


    function checkValueInRange(o, n, min, max) {

        if (o.val() < min || o.val() > max) {

            o.addClass("ui-state-error");

            updateTips("value of " + n + " must be between " + min + " and " + max + ".");

            return false;

        } else {

            return true;

        }

    }


    function checkRegexp(o, regexp, n) {

        if (!(regexp.test(o.val()))) {

            o.addClass("ui-state-error");

            updateTips(n);

            return false;

        } else {

            return true;

        }

    }


    $("#contrast-dialog-form").dialog({

        autoOpen: false,

        height: 210,

        width: 250,

        modal: true,

        buttons: {

            "Apply": function () {

                var bValid = true;

                allFields.removeClass("ui-state-error");

                bValid = bValid && checkValueInRange(value, "value", -255, 255)

                //bValid = bValid && checkLength(value, "value", 1, 4);

                bValid = bValid && checkRegexp(value, /^-?\d+$/, "value must be a number.");


                if (bValid) {

                    $("#history tbody").append("<tr>" +
                            "<td>" + "Contrast adjustment" + "</td>" +
							"<td>" + value.val() + "</td>" +
						    "</tr>");

                    document.getElementById("iframe").src = "ImageViewer.aspx?process=contrast" + "&&value=" + value.val();

                    $(this).dialog("close");

                    iFrameRefresh();

                }

            },

            Cancel: function () {

                $(this).dialog("close");

            }

        },

        close: function () {

            allFields.val("").removeClass("ui-state-error");

        }

    });


 $("#contrast-adjustment")
			.button()
			.click(function () {
			    $("#contrast-dialog-form").dialog("open");
			});

});


$(function () {


    $("#grayscale")
			.button()
			.click(function () {

			    document.getElementById("iframe").src = "ImageViewer.aspx?process=grayscale&&value=0";
			    iFrameRefresh();
			    $("#history tbody").append("<tr>" +
                            "<td>" + "Grayscale" + "</td>" +
							"<td>" + "-" + "</td>" +
						    "</tr>");
			});

});

$(function () {


    $("#invest")
			.button()
			.click(function () {
			    document.getElementById("iframe").src = "ImageViewer.aspx?process=invest&&value=0";
			    iFrameRefresh();
			    $("#history tbody").append("<tr>" +
                            "<td>" + "Invest" + "</td>" +
							"<td>" + "-" + "</td>" +
						    "</tr>");
			});

});

$(function () {
    $("#resize-dialog:ui-dialog").dialog("destroy");
    var width = $("#tbxResizeValue_Width"), height = $("#tbxResizeValue_Height"),
			allFields = $([]).add(width).add(height),
			tips = $(".validateTips");

    function updateTips(t) {
        tips
				.text(t)
				.addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }
    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkValueInRange(o, n, min, max) {
        if (o.val() < min || o.val() > max) {
            o.addClass("ui-state-error");
            updateTips("value of " + n + " must be between " + min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#resize-dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 250,
        modal: true,
        buttons: {
            "Apply": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");
                bValid = bValid && checkValueInRange(width, "width", 10, 1000)
                bValid = bValid && checkRegexp(width, /^-?\d+$/, "width must be a number.");
                bValid = bValid && checkValueInRange(height, "height", 10, 1000)
                bValid = bValid && checkRegexp(height, /^-?\d+$/, "height must be a number.");
                if (bValid) {
                    $("#history tbody").append("<tr>" +
                            "<td>" + "Resize" + "</td>" +
							"<td>" + width.val() + "x" + height.val() + "</td>" +
						    "</tr>");
                    document.getElementById("iframe").src = "ImageViewer.aspx?process=resize" + "&&value=" + width.val() + "x" +height.val();
                    $(this).dialog("close");
                    iFrameRefresh();
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#resize")
			.button()
			.click(function () {
			    $("#resize-dialog-form").dialog("open");
			});
});

$(function () {

    $("#brightness-dialog:ui-dialog").dialog("destroy");


    var value = $("#tbxValue"),
			allFields = $([]).add(value),
			tips = $(".validateTips");


    function updateTips(t) {

        tips
				.text(t)
				.addClass("ui-state-highlight");

        setTimeout(function () {

            tips.removeClass("ui-state-highlight", 1500);

        }, 500);

    }

    function checkLength(o, n, min, max) {

        if (o.val().length > max || o.val().length < min) {

            o.addClass("ui-state-error");

            updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");

            return false;

        } else {

            return true;

        }

    }


    function checkValueInRange(o, n, min, max) {

        if (o.val() < min || o.val() > max) {

            o.addClass("ui-state-error");

            updateTips("value of " + n + " must be between " + min + " and " + max + ".");

            return false;

        } else {

            return true;

        }

    }


    function checkRegexp(o, regexp, n) {

        if (!(regexp.test(o.val()))) {

            o.addClass("ui-state-error");

            updateTips(n);

            return false;

        } else {

            return true;

        }

    }


    $("#brightness-dialog-form").dialog({

        autoOpen: false,

        height: 210,

        width: 250,

        modal: true,

        buttons: {

            "Apply": function () {

                var bValid = true;

                allFields.removeClass("ui-state-error");

                bValid = bValid && checkValueInRange(value, "value", -255, 255)

                //bValid = bValid && checkLength(value,"value",1,4);

                bValid = bValid && checkRegexp(value, /^-?\d+$/, "value must be a number.");


                if (bValid) {

                    $("#history tbody").append("<tr>" +
                            "<td>" + "Brightness adjustment" + "</td>" +
							"<td>" + value.val() + "</td>" +
						    "</tr>");

                    document.getElementById("iframe").src = "ImageViewer.aspx?process=brightness" + "&&value=" + value.val();

                    $(this).dialog("close");

                    iFrameRefresh();

                }

            },

            Cancel: function () {

                $(this).dialog("close");

            }

        },

        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }

    });

    $("#brightness-adjustment")
			.button()
			.click(function () {
			    $("#brightness-dialog-form").dialog("open");
			});

});

$(function () {
    $("#rotateflip-dialog:ui-dialog").dialog("destroy");

    $("#rotateflip-dialog-form").dialog({
        autoOpen: false,
        height: 250,
        width: 250,
        modal: true,
        buttons: {
            "Apply": function () {
                var value = $("#selectable option:selected").val();

                $("#history tbody").append("<tr>" +
                        "<td>Rotate and Flip</td>" +
                		"<td>" + $("#selectable option:selected").val() + "</td>" +
                		"</tr>");
                document.getElementById("iframe").src = "ImageViewer.aspx?process=rotateflip&&value=" + $("#selectable option:selected").val();
                $(this).dialog("close");
                iFrameRefresh();
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#rotate-flip")
			.button()
			.click(function () {
			    $("#rotateflip-dialog-form").dialog("open");
			});
});
